
export function toObject(jsonStr) {
  try {
    const data = JSON.parse(jsonStr);
    for (let k in data) {
      data[k] = toObject(data[k]);
    }
    return data;
  } catch {}
  return jsonStr;
}

export function parseUrlParams(url) {
  var ret = {};
  const query = decodeURIComponent(url).substring(1);
  if(query.length == 0) return ret;
  const vars = query.split("&");
  for(var i in vars) {
    const item = vars[i].split('=');
    if(item.length == 0) continue;
    ret[item[0]] = item.length>1?toObject(item[1]):'';
  }
  return ret;
}

export function parseHrefUrlParams(field, search) {
  var ret = parseUrlParams(window.location.search)
  return ret[field];
}

export function makeUrlParams(data) {
  if(!data) return '';
  let ret = '?';

  for(let key in data) {
    ret += `${key}=${data[key]}&`;
  }

  return ret.substring(0, ret.length-1);
}